package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    /**
     * 查询频道(分页查询)
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        LambdaQueryWrapper<AdChannel> queryWrapper = null;
        //1: 校验参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2：封装分页查询条件

        //2.1:分页条件
        IPage channelPage = new Page(dto.getPage(), dto.getSize());
        //2.2: 查询条件
        if (StringUtils.isNoneBlank(dto.getName())) {
            queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.like(AdChannel::getName, dto.getName());
        }
        //3，查询
        this.page(channelPage, queryWrapper);
        //4：封装结果返回
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), channelPage.getTotal(), channelPage.getRecords());
        return result;
    }

    /**
     * 新增频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult addChannel(AdChannel adChannel) {
        //1: 数据校验
//        if (adChannel == null) {
//            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
//        }
        //2: 检查频道名是否已经存在
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(AdChannel::getName, adChannel.getName());
        if (this.getOne(lambdaQueryWrapper) != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道名称已经存在");
        }

        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        return ResponseResult.okResult();
    }


    /**
     * 修改频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(AdChannel adChannel) {
        //1: 数据校验
//        if (adChannel == null) {
//            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
//        }
        //2: 检查频道名称是否发生变化
        //校验要存储的数据是否已经存在
        AdChannel channel = this.getById(adChannel);
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //校验频道名称是否发生变化
        if (adChannel.getName() != null && !channel.getName().equals(adChannel.getName())) {
            //如果发生了变化
            if (this.getOne(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName())) != null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道名称已存在");
            }
        }
        //3: 修改数据
        adChannel.setCreatedTime(new Date());
        this.updateById(adChannel);
        return ResponseResult.okResult();
    }


    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteChannel(Integer id) {
        //1: 校验数据
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //校验要删除的数据是否存在
        AdChannel channelOld = this.getById(id);
        if (channelOld == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //校验要删除的数的状态（状态可用不可删除）
        if (channelOld.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //删除数据
        if (this.removeById(id)) {
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }

}
